from typing import List


class Solution:
    def subarraySum(self, nums: List[int]) -> int:
        pre = [0] * (len(nums) + 1)
        for i in range(len(nums)):
            pre[i + 1] = pre[i] + nums[i]
        left = [0] * len(nums)
        # 变长数组的左区间  右区间固定为i
        for i in range(len(nums)):
            left[i] = max(0, i - nums[i])
        ans = 0
        for i in range(len(nums)):
            ans += pre[i + 1] - pre[left[i]]
        return ans
